<script type="text/javascript">
	var msgbox = $('#msgBox');
	
	function my_upd(wtf, val){
        $('div#'+wtf).html('<img src="images/loading.gif" />');
        $.post('{base_url}control/'+wtf,
        {date: val, group: 'true'},
        function(data){
                if(data == "")
                {
                        $('div#my_'+wtf).text('Нет записей.');
                }
                else
                {
			$('div#my_'+wtf).html("<table id='tbl' class='t_ajax'> \
			<thead> \
				<tr> \
					<th id='datetime'>Дата и время</td>\
					<th id='subj'>Название вебинара</td>\
					<th id='control'>Действие</td>\
				</tr> \
			 </thead>\
			 <tbody> \
			</tbody> \
			</table> ");
			$.each(data, function(i, v){
                                if(wtf == 'planned')
                                {
					var dt = new Date();
					var day = dt.getDate();
					if(day < 10) day = '0'+day;
					var month = dt.getMonth()+1;
					if(month < 10) month = '0'+month;
					var year = dt.getFullYear();
					var date = day+"."+month+"."+year;
					var time = dt.getHours()*60 + dt.getMinutes();
					
                                        var control = $('<div>');
					control.append($('<div id="'+v['ID']+'" class="'+wtf+'">Начать</div>'));
					control.append($('<div id="e'+v['ID']+'" class="edit_'+wtf+'">Редактировать</div>'));
					if(v['status'] == 'Закрытый')
						control.append($('<div id="a'+v['ID']+'" class="add_'+wtf+'">Добавить слушателей</div>'));
					control.append("<input id='i"+v['ID']+"' type='hidden' value='"+$.JSON.encode(v)+"'/>");
					var but0n = control.find('div.'+wtf);
					
                                        if(v['date'] === date)
                                        {
						var stime = parseTime(v['stime']);
						if(stime.mm < 30) stime.hh-=1, stime.mm-=30;
						else stime.mm-=30;
						stime = stime.hh*60 + stime.mm;
						var etime = parseTime(v['etime']);
						etime = etime.hh*60 + etime.mm*1;
						if(etime < stime) etime += 24*60;
						if(!(time >= stime && time <= etime))
							but0n.addClass('disabled');
                                        }
                                        else
                                        {
						but0n.addClass('disabled');
                                        }
					v['control'] = control.html();
                                }
                                else
                                v['control'] = "<div id="+v['ID']+" class='"+wtf+"'>Копировать</div> \
                                                <input id='i"+v['ID']+"' type='hidden' value='"+$.JSON.encode(v)+"'/>";
                        });
                        $('#my_'+wtf).tabTmpl(data);
                        var bt = $('div.'+wtf);
			var ebt = $('div.edit_'+wtf);
			var abt = $('div.add_'+wtf);
                        switch(wtf)
                        {
                                case 'past':
                                             bt.click(function(){
                                                     var id = $(this).attr('id');
                                                     $.post('{base_url}module/gadget/'+wtf,
                                                        {json: $('input#i'+id).val()},
                                                        function(data){
                                                                msgbox.html(data);
                                                                msgbox.dialog({autoOpen: true, width: 300,
										height: 'auto', title: 'Копирование'});
                                                        },'html');
                                             });
                                             break;
                                case 'planned':
                                             var id = bt.attr('id');
					     if(! /disabled/.test(bt.attr('class')))
						bt.click(function(){
							//document.location.href = '{base_url}control/create/'+id;
							$.get('{base_url}control/create/'+id,
								function(res){
									$.gEmit('menu', 'planned');
									$.gEmit('updRn');
								});
						});
					     ebt.click(function(){
                                                     var id = $(this).attr('id');
						     var pat = /\d+/;
						     id = pat.exec(id);
                                                     $.post('{base_url}module/gadget/edit',
                                                        {json: $('input#i'+id).val()},
                                                        function(data){
                                                                msgbox.html(data);
                                                                msgbox.dialog({autoOpen: true, width: 400,
									height: 'auto', title: 'Редактирование',
									close: function () { $.gEmit('menu', 'planned'); }});
                                                        },'html');
                                             });
					     abt.click(function(){
						     var id = $(this).attr('id');
						     var pat = /\d+/;
						     id = pat.exec(id);
						     msgbox.html('<iframe src="http://reg.vvsu.ru/admin/rights/rights.asp?ID_PROJECT=106&ID_ROLE=758&ID_OBJECT='+id+'" width=765px height=530px></iframe>');
						     msgbox.dialog({
								autoOpen: true,
								width: 800,
								height: 600,
								title: 'Система Назначения Прав',
								close: function(event, ui){my_upd('planned', '');}
								});
                                             });
                                             break;
                                default: break;
                        }
                 }
        }, 'json');
}

	$(document).ready(function(){
		var container = '{container}';
		var MyWeb = $('#'+container);
		MyWeb.delegate('','menu', function(ev, wtf){
					$('.menu#menu_'+wtf).trigger('click');
			});
                var dtpkr = $("#dtpkr");
                var date = new Date();

		dtpkr.datepicker({dateFormat: 'yy-mm-dd', firstDay: 1, onSelect: function(dateText, inst){
			//my_upd(wtf, dateText);
		}, minDate: new Date(date.getFullYear(), date.getMonth(), date.getDate())});

		dtpkr.find('.ui-datepicker').css('border', '0px');

		var menu = $('div#calendar div.menu');
		menu.click(function(){
			var msel = $(this)
			$('div#calendar div.menu.mselected').removeClass( "mselected" );
			msel.addClass("mselected");
			var id = msel.attr('id');
			switch(id)
			{
				case 'menu_planned':
					$('#myWeb').html('<div id="my_planned"></div>');
					my_upd('planned', '');
					break;
				case 'menu_past':
					$('#myWeb').html('<div id="my_past"></div>');
					my_upd('past', '');
					break;
				case 'menu_add':
					$('#myWeb').html('<div id="my_add"></div>');
					$('#my_add').load('{base_url}module/tpl/addToPlan');
					break;
				default:
					break;
			}
		});
		if(!$.browser.webkit) $('#calendar_head').css('height','65px'); // Fix
		$('#myWeb').html('<div id="my_planned"></div>');
		my_upd('planned', '');
        });
</script>
<div id="calendar_head"></div>
<div id="menu_planned" class="menu mselected">Мои запланированные</div>
<div id="menu_past" class="menu">Мои прошедшие</div>
<div id="menu_add" class="menu">Добавить вебинар</div>
<div id="dtpkr" style="margin-top: 20px; direction: ltr; float: right;"></div>
<br />
